package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineApproveItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.DrafRemListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.SanctnListItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ApproveDetailThread extends BasicThread {

	public interface ApproveDetailThreadCallback {
		public void OnApproveDetailCompleted(boolean isNetworkConntected
				, QuarantineApproveItem approveItem, String message);
		public void OnApproveDetailError(ErrorItem errorItem);
	}

	private String url;
	private ApproveDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String recNo;
	private String offCd;
	private String value;

	public ApproveDetailThread(Context context, ApproveDetailThreadCallback callback
			, String offCd, String recNo, String value, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.recNo = recNo;
		this.offCd = offCd;
		this.value = value;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		if(value.equals("0")){
			post =  ParameterUtil.getQuarantineApproveDetailParams(offCd, recNo);	
			map.put(Const.SEARCHRESOFFCD, offCd);
			map.put(Const.SEARCHRESNO, recNo);
			param = parameterReturn(Const.REQ012, (HashMap<String, String>) map);
		}else{
			post =  ParameterUtil.getQuarantineApproveEnblocParams(offCd);
			map.put(Const.FULLRESNO, offCd);
			param = parameterReturn(Const.REQ039, (HashMap<String, String>) map);
		}
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnApproveDetailCompleted(false, null, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnApproveDetailError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnApproveDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
				JSONObject quarantine = arraydisinfection.getJSONObject(0);
				
				QuarantineApproveItem approveItem = new QuarantineApproveItem();
				
				ArrayList<DrafRemListItem> arrayDraf = new ArrayList<DrafRemListItem>();
				JSONArray items	 	= json.getJSONArray(QuarantineApproveItem.DRAFREMLIST);
				for(int i = 0; i < items.length(); i++) {
					JSONObject itemObject = items.getJSONObject(i);
					DrafRemListItem item = new DrafRemListItem();
					item.setCode(BrUtilManager.isNullCheck(itemObject.getString(DrafRemListItem.CODE)));
					item.setCodeNm(BrUtilManager.isNullCheck(itemObject.getString(DrafRemListItem.CODENM)));
					arrayDraf.add(item);
				}
				approveItem.setDrafRemList(arrayDraf);
				
				int sanCnt = json.getInt(QuarantineApproveItem.SANCTNLISTRESULT);
				ArrayList<SanctnListItem> arraySan = new ArrayList<SanctnListItem>();
				if(sanCnt > 0){
					JSONArray sanitems = json.getJSONArray(QuarantineApproveItem.SANCTNLIST);
					for(int i = 0; i < sanitems.length(); i++) {
						JSONObject itemObject = sanitems.getJSONObject(i);
						SanctnListItem item = new SanctnListItem();
						item.setKwaCd(BrUtilManager.isNullCheck(itemObject.getString(SanctnListItem.KWACD)));
						item.setKwaNm(BrUtilManager.isNullCheck(itemObject.getString(SanctnListItem.KWANM)));
						arraySan.add(item);
					}
				}
				approveItem.setSanctnList(arraySan);
				
				approveItem.setFormNo(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.FORMNO)));
				approveItem.setOffCd(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.OFFCD)));
				approveItem.setRecNo(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.RECNO)));
				approveItem.setFullResNo(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.FULLRESNO)));
				approveItem.setResDe(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.RESDE)));
				approveItem.setResultProgrsNm(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.RESULTPROGRSNM)));
				approveItem.setProgrsNm(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.PROGRSNM)));
				approveItem.setDrafCd(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.DRAFCD)));
				approveItem.setDrafBody(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.DRAFBODY)));
				approveItem.setProgrsSt(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.PROGRSST)));
				approveItem.setHandleKwa(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.HANDLEKWA)));
				approveItem.setDrafRem(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.DRAFREM)));
				approveItem.setEdiYn(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.EDIYN)));
				approveItem.setDrafDate(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.DRAFDATE)));
				approveItem.setBodySe(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.BODYSE)));
				approveItem.setFieDe(BrUtilManager.isNullCheck(quarantine.getString(QuarantineApproveItem.FIEDE)));
				callback.OnApproveDetailCompleted(true, approveItem, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}
